package com.example.demo.service;

import com.example.demo.model.Notification;
import com.example.demo.repository.NotificationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class NotificationService {
    @Autowired
    public NotificationRepository repo;
    @Autowired
    JavaMailSender mailSender;

    public void sendNotification(Long userId, String title, String content, String type, String email) {
        Notification n = new Notification();
        n.setUserId(userId);
        n.setTitle(title);
        n.setContent(content);
        n.setType(type);
        n.setTime(LocalDateTime.now());
        repo.save(n);

        // 邮件通知（可选）
        SimpleMailMessage msg = new SimpleMailMessage();
        msg.setTo(email);
        msg.setSubject("[通知] " + title);
        msg.setText(content);
        mailSender.send(msg);
    }
}